package com.htu.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.htu.constant.CResultType;
import com.htu.domain.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



@Controller
public class Alipay
{
    private final String APP_ID = "2021000119651427";
    //应用私钥
    private final String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCeAwDlklEjann/5y5dp0aWmt54lMTwyzad8h4OsN208uaKdSKTV8llqq8ETd5JYSVTUlt9F//a15LkU/ip1MJxbcrzxLOmXsdMLUtaSjqD7qgxYKXe3eluPKpYQZW8f2/z28H2Nc37CtYGW7FRvp19BPg6pINtk+z8EZiPLo2nDQzCXpZa3mJVWX83jl+C9eN86gTtv0fD6nogAHRNpcebcsvjrNmS7WYtCf90YA4cErsQssgAwB0UREQZ6EEQ44cgKlSpv+Rq3FRN20GO0em/nDxpK0DASBsCvNGjCE7PvFCMbuQe3qpumwGX+1LbRZrSNiuDr942ff6UC8JdBHQ1AgMBAAECggEBAJPW75HcMnpWd0tNe/44Mj1bLWfYmeNJ3xIspJ7OMMHKupU1P2JospCjABmg2agCcq/g0KTcPZD+PfvP7UrJPBxww4EMu6Gf8AyAHI6gANvMAiYg7VD2hB+WNzNYOcch3Rdmt7WrZcvKubE9PUzkcem1KqODpUxzKTo3JGxi/Doc6vISaHKvyEBT3hlIZolxojsEPaTUYUo1RH+L1dIBSXzrPNglTAqSZiVg9qHIzmTHT6tgGtoJ8NUlhBKRIJYRffwW1D38tc5iniRHhJ6DK17cLMFpY8iVGH+o3RuVys6Lj05XgGXNrY/ujt8S1S9iVHyLzsal71h0E4FbW5eTfwkCgYEA4MGary/ocLVt6tRn2gKFzeWT3+VI+T6cnYRzRWu/f1dVkrFNamx3m1mG7gteaiVAwVISqvK+aCpM6bUErAPe7tAsXFoRhTNUIh7IGxfE97Sa5gd2IA5jVL3SMFTyzOGpNzcm/74YQw/SHJoXJQvD5LmNMO+NzuAzVP2qsMMGmRsCgYEAs/opxQ/sOHtJ5gLh2kEE/4C/fiWvzcEWUtKCXUItgljivdq7RAgl6f0wq8e/MxES1fiCC2TgOEuXioXMocEiNBteFZ21BPMk+uugKOt3SDjqOfocCzPMRXXVPiXd+HsLbzIZadRJk0JPoBb+DeNQkHOFyWmWV0DVtJQJn98uzO8CgYBvL7e7NMMTht2Cj2N02UyiSBF4cLBCBTRrREvWKvCIKYc+05GNtW0SIB79AgjKD/XR7S0o/n8tfsef0homkp+lmrxnT3NdZsAXRQ0PQhoBxInghA5u209Oa71ghZW7v2L72r/jQoI0Is55sc0DWaemLhjn/Yo0rnsWb4sSeNh5wwKBgEMJjGNtkeRb7+Ov8SZVj6IH98vqB7jREhqZdJt+GetyZkqnXZdynYSxnV5u0YYY9vYiUXYjBe2SIokjW1xozu+vvfS5QILuejjIaWi0hEKviHc99RIrIgxBXsN+YVa7pj+t3o9v1lP2oCgWSF0t7YVQsmWaYbhwWumfNpKJFCqbAoGAUo1AEGIkQMKBElpR/wy2mHRfnhaMqCQAuJ6hhbY1QmtQVUnbJUuPxN2wC1sR2CW3M3lyUkKwpzXLMcaPRFul5JcRdc0yoZHvryi1omMrxpG649+AogDFH2/z2q2Pc05qX97U8LUe/RNggqgH5LnnIK5BaZx8OUh/G2lZOg5GJ6s=";
    private final String CHARSET = "UTF-8";
    // 支付宝公钥
    private final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjP+77n1pbhf74+yZ64Ba6IzLEmbCD+tta+WiHtlmQzt0lOdJhupHwtv/vNGKr59+KTbS97sy33uXUO5PHW0n7kjulbqygiQ1j83CL9c3Aqm02sILc0LVZXzVw2QrUDHf5tgKtx7P69wHd0Vu6ap+AIjeWAr01JiZaIRXrU4dlKqN3Tgy6IyEu7PMbzVh2cRAms9Y23Tl4Pdpo76pO/GNWDgEPDx5egJlpbcnbEcTiMnxEHsc1zghWf5LaTgAP7SxWpzdk0xoAlJuhTYHoP4IZ+sJ7PWsWKjEzCekFbO7OVnRJKi0bBzpaSLC9xGmqKY+ppbjpKT6IazR57G69OEiYwIDAQAB";
    //这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do
    private final String GATEWAY_URL = "https://openapi.alipaydev.com/gateway.do";
    private final String FORMAT = "JSON";
    //签名方式
    private final String SIGN_TYPE = "RSA2";
    //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
    private final String NOTIFY_URL = "http://127.0.0.1/notifyUrl";
    //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
    private final String RETURN_URL = "http://localhost:8088/#/ZhiFuBao";


    @RequestMapping("/AliPay")
    @ResponseBody
    public Result<String> alipay(String goods, Double done_money, String done_id) throws AlipayApiException
    {
        //调用封装好的方法（给支付宝接口发送请求）
        int resultType = CResultType.FAIL;
        String payForm = sendRequestToAlipay(done_id, done_money, goods);
        if (payForm.length() != 0)
        {
            resultType = CResultType.SUCCESS;
        }
        return new Result<String>(resultType,done_id,payForm);
    }

    /*
参数1：订单号
参数2：订单金额
参数3：订单名称
 */
    //支付宝官方提供的接口
    private String sendRequestToAlipay(String outTradeNo, Double totalAmount, String subject) throws AlipayApiException
    {
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(RETURN_URL);
        alipayRequest.setNotifyUrl(NOTIFY_URL);

        //商品描述（可空）
        String body = "";
        alipayRequest.setBizContent("{\"out_trade_no\":\"" + outTradeNo + "\","
                + "\"total_amount\":\"" + totalAmount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        return result;
    }
}
